package com.example.localvideoplayer

import android.graphics.Color
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Window
import android.view.WindowManager
import android.widget.MediaController
import android.widget.VideoView

class VideoViewActivity : AppCompatActivity() {
    private lateinit var videoView: VideoView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (supportActionBar != null) {
            supportActionBar?.hide()
        }
        window.addFlags(Window.FEATURE_NO_TITLE)

        val window = window
        window.statusBarColor = Color.TRANSPARENT  //设置状态栏颜色透明
        window.navigationBarColor = Color.TRANSPARENT//设置导航栏颜色透明
        window.setFlags(
            android.R.style.Theme_NoTitleBar_Fullscreen,
            android.R.style.Theme_Material_NoActionBar_TranslucentDecor
        )

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        setContentView(R.layout.activity_video_view)

        val uriName = intent.getStringExtra("uri")
        if (uriName != null) {
            bindViews(uriName)
        }
    }

    private fun bindViews(uriName: String) {
        videoView = findViewById(R.id.videoView)

        videoView.setVideoURI(Uri.parse(uriName))
        /**
         * 为 VideoView 视图设置媒体控制器，设置了之后就会自动由进度条、前进、后退等操作
         */
        videoView.setMediaController(MediaController(this))
        /**视频准备完成时回调
         */
        videoView.setOnPreparedListener {
            Log.i(
                "tag",
                "--------------视频准备完毕,可以进行播放......."
            )
        }
        /**
         * 视频播放完成时回调
         */
        videoView.setOnCompletionListener {
            Log.i("tag", "------------------视频播放完毕..........")
            /**播放完成时，再次循环播放 */
            /**播放完成时，再次循环播放 */
            /**播放完成时，再次循环播放 */
            /**播放完成时，再次循环播放 */
            videoView.start()
        }
        /**
         * 视频播放发送错误时回调
         */
        videoView.setOnErrorListener { mp, what, extra ->
            Log.i("tag", "---------------------视频播放失败...........")
            false
        }
        /**开始播放视频
         */
        videoView.start()
    }
}